<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Machine;
use App\Models\FreightWay;
use App\Models\MachineManager;
use DB;
use App\Exports\defectExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\Orders;



class StatisticsController extends Controller
{
    //缺货商品
    public function outGoods(Request $request, FreightWay $freightWay, Machine $machine)
    {
    	$user = auth('admin')->user();
    	if($user->type == 1){
    		$machines = $machine->pluck('id');
    	}else{
    		$machines = optional($user->machine)->pluck('id');
    	} 
    	$freights = $freightWay->with(['goods' => function($query){
    		$query->select('id', 'goods_name','goods_image','domainName');
    	}])->whereIn('machine_id', $machines)
    	   ->where('goods_id', '<>', 0)
    	   ->whereRaw('capacity > stock')
    	   ->select('*',DB::raw("sum(capacity) as capacity_num"),DB::raw("sum(stock) as stock_num"))
    	   ->groupBy('goods_id')
    	   ->orderBy('id', 'desc')
    	   ->paginate(config('api.page.limit'));
    	return view('admins.statistics.goods', compact('freights'));
    }
    //导出缺货商品
    public function putGoodsExcel(Request $request, defectExport $defectExport)
    {

    	return $defectExport->withinDays(date("Y-m-d"));
    }

    //订单统计
    public function orders(Request $request, Orders $orders)
    {
		return view('admins.statistics.orders');
    }
    //导出订单数据
    public function ordersExcel()
    {

    }
}
